 /* Copyright (C)
  * 2024 ZKLX Optoelectronic Technology Co.LTD
  * All rights reserved.
  */
 /**
  * @file web_viewer.cpp
  * @brief jcvp web engine
  * @author xuke
  * @version 1.0
  * @date 2024-10-15
  */

#include "/home/xuke/bin/dbg.h"
#include <iostream>
#include <fstream>
#include <thread>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <nlohmann/json.hpp>
#include "jcvp.h"

using njson = nlohmann::json;

/*--------------------------------extern--------------------------------------------*/

/*--------------------------------globals-------------------------------------------*/
JCVP_T *g_jcvp_obj = NULL;

/*--------------------------------locals--------------------------------------------*/

/*--------------------------------funcs---------------------------------------------*/
// json ctrl reciver
static void jcvp_jc_recive_task(void *args)
{
	std::string jc_str;
	char *cmd_type = NULL;
	uint32_t echo_cnt = 0;
	uint32_t recv_size = 0;
	int32_t last_index = 0;
	std::ofstream *output_file = NULL;
	std::string file_path;

	while (true) {
		if (!g_jcvp_obj->_conn) { //is connected
			sleep(1);
			continue;
		}

		jc_str = g_jcvp_obj->jc_recv();
		//log_d("jc:[%s]", jc_str.c_str());

		/* parase json data */
		njson root_obj = njson::parse(jc_str);
		if (root_obj["type"] == "top_ctrl") {
			if (root_obj["cmd"] == "autoAgent_en") {
				printf("recv autoAgent_en: %d\n", (bool)root_obj["autoAgent_en"]);
			}
		} else if (root_obj["type"] == "fire_mode") {
			if (root_obj["cmd"] == "jc_fire_auto") {
				printf("recv jc_fire_auto buttom\n");
			} else if (root_obj["cmd"] == "jc_fire_request") {
				printf("recv jc_fire_request buttom\n");
			} else if (root_obj["cmd"] == "jc_fire_simulate") {
				printf("recv jc_fire_simulate buttom\n");
			} else if (root_obj["cmd"] == "jc_fire_null") {
				printf("recv jc_fire_null buttom\n");
			}
		} else if (root_obj["type"] == "test_bt") {
			if (root_obj["cmd"] == "wind_test") {
				printf("recv wind_test buttom\n");
			}
		}
	}
}

// json status sender
static void jcvp_js_update_task(void *args)
{
	float test_atp_yaw = 0.0;
	int test_index=-1;
	srand((unsigned int)time(NULL));
	njson radar_obj;
	while (true) {
		if (g_jcvp_obj->_conn) { //is connected
			if (1) { //run state
				njson root_obj;
				root_obj["type"] = "run_state";

				//雷达数据
				if (test_index == -1) {
					radar_obj.push_back(njson::parse(R"({"targetId":1, "aziAngle":  67.589, "pitchAngle": 11.489, "distance": 1973.800, "motionDirection": 0, "speed": 6.880,  "time": 1742787856724})"));
					radar_obj.push_back(njson::parse(R"({"targetId":2, "aziAngle": 123.589, "pitchAngle": 11.489, "distance": 1573.800, "motionDirection": 1, "speed": 7.880,  "time": 1742787856724})"));
					radar_obj.push_back(njson::parse(R"({"targetId":3, "aziAngle": 187.589, "pitchAngle": 11.489, "distance": 1173.800, "motionDirection": 2, "speed": 8.880,  "time": 1742787856724})"));
					radar_obj.push_back(njson::parse(R"({"targetId":4, "aziAngle": 224.589, "pitchAngle": 11.489, "distance":  773.800, "motionDirection": 1, "speed": 9.880,  "time": 1742787856724})"));
					radar_obj.push_back(njson::parse(R"({"targetId":5, "aziAngle": 289.589, "pitchAngle": 11.489, "distance":  473.800, "motionDirection": 2, "speed": 10.880, "time": 1742787856724})"));
				}
				if (test_index > 5) {
					/* ZklxTag: xuke modified on 2025/03/24 for fixing M# FIXME: for test */
					for (int i=0; i<radar_obj.size(); i++) {
						radar_obj[i]["aziAngle"] = (float)radar_obj[i]["aziAngle"] + ((rand()%21)-10);
						radar_obj[i]["distance"] = (float)radar_obj[i]["distance"] + ((rand()%21)-10);
					}
					test_index = 0;
				} else {
					test_index++;
				}
				root_obj["radar_obj"] = radar_obj;
				//ATP数据
				test_atp_yaw = (test_atp_yaw>360.0)?0.0:test_atp_yaw+1.0;
				njson atp_obj;
				atp_obj["pitch"] = 12.345;
				atp_obj["yaw"] = test_atp_yaw;
				atp_obj["itv_view_angle"] = 5.00; // (arctan(pixSize*pixNum/itv_zoom))/2
				atp_obj["ctv_view_angle"] = 8.00;
				atp_obj["track_tv"] = "itv";
				atp_obj["is_fire"] = false;
				root_obj["atp_obj"] = atp_obj;
				//AUTO_AGENT状态数据
				njson agent_obj;
				agent_obj["sm_state"] = 3;
				root_obj["agent_obj"] = agent_obj;
				//图像板CV状态数据
				njson cv_obj;
				cv_obj["itv_cv_state"] = njson::parse(R"({"state":"Tracking", "score": 78, "target":{"x":305, "y":241, "w":30, "h":30}, "offset":{"x":-2, "y":3}})");
				cv_obj["ctv_cv_state"] = njson::parse(R"({"state":"Searching", "score": 0, "target":{"x":960, "y":512, "w":50, "h":30}, "offset":{"x":-2, "y":3}})");
				cv_obj["ftv_cv_state"] = njson::parse(R"({"state":"Tracking", "score": 75, "target":{"x":340, "y":360, "w":120, "h":80}, "offset":{"x":-1, "y":1}})");
				root_obj["cv_obj"] = cv_obj;

				g_jcvp_obj->jc_send(root_obj.dump());
			}
		}

		usleep(40*1000);
	}
}

int web_viewer_init(std::string web_root, int port, int fps, float img_scaling)
{
	// init jcvp_web server
	g_jcvp_obj = new JCVP_T(web_root, port, fps, img_scaling); //web_root, fps, img_scaling
	g_jcvp_obj->webs_run();

	// jc task
	std::thread jcvp_jc_thread(jcvp_jc_recive_task, nullptr);
	jcvp_jc_thread.detach();

	// js task
	std::thread jcvp_js_thread(jcvp_js_update_task, nullptr);
	jcvp_js_thread.detach();

	return 0;
}

